// 定义数据格式
var parent_index;
var child_index;

Component({
  /**
   * 组件的属性列表
   */
  properties: {

    list: {
      type: Object,
      value: {}
    },
    deletetd: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    subMenuDisplay: [],//控制展开/收起子级内容

    startX: 0, //开始坐标
    startY: 0,
    TouchMove:[],//控制右滑删除
  },
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名

  attached: function () {
    console.log('emmm1', 'attached');
    
  },

  moved: function () {
    console.log('emmm2', 'moved');
  },

  detached: function () {
    console.log('emmm3', 'detached');
  },
  /**
   * 组件的方法列表
   */
  methods: {
    initSubMenuDisplay: function() {

      var travels = this.properties.list.travel;

      var ary = [];
      for (var i = 0; i < travels.length; i++) {
        var obj = true;
        ary.push(obj)
      }
      this.setData({
        subMenuDisplay: ary
      })
    },
    initTouchMove:function(){

      var travels = this.properties.list.travel;
      var ary_outer = [];

      for (var i = 0; i < travels.length; i++) {
        var ary_inner = [];
        if (travels[i].places.length>0){
          for (var j = 0; j < travels[i].places.length;j++){
            var obj = false;
            ary_inner.push(obj);
          }
        }
        ary_outer.push(ary_inner);
      }

      this.setData({
        TouchMove: ary_outer
      })
    },
    //展示/收起 子级
    showFrom: function(e) {

      console.log(e.currentTarget.dataset.param);

      var ary = this.data.subMenuDisplay;
      var index = e.currentTarget.dataset.param;

      var obj = ary[index];

      ary[index] = !obj;
      this.setData({
        subMenuDisplay: ary
      })

    },
    //手指刚放到屏幕触发 
    touchS: function(e) {

      this.setData({
        startX: e.changedTouches[0].clientX,
        startY: e.changedTouches[0].clientY,
      })
    },
    //触摸时触发，手指在屏幕上每移动一次，触发一次 
    touchM: function(e) {

      var that = this;

      var parent = e.currentTarget.dataset.parent; //父级的索引
      var index = e.currentTarget.dataset.index; //当前索引

      var startX = that.data.startX; //开始X坐标
      var startY = that.data.startY; //开始Y坐标

      var touchMoveX = e.changedTouches[0].clientX; //滑动变化坐标
      var touchMoveY = e.changedTouches[0].clientY; //滑动变化坐标

      var angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
 
      console.log(parent, index);
      var obj = this.data.TouchMove[parent][index];

      if (touchMoveX > startX){
        obj = false;
      }else{
        obj = true;
      }
     
      var touchMove_item = 'TouchMove['+parent+']['+index+']';
      this.setData({
        [touchMove_item]: obj
      })
      console.log(this.properties.list.travel);
      
      parent_index = parent;
      child_index = index;
    },

    angle: function(start, end) {
      var _X = end.X - start.X,
        _Y = end.Y - start.Y
      //返回角度 /Math.atan()返回数字的反正切值
      return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
    },
    delItem: function(e) {

      var obj = {
        parent: parent_index,//决定是景点还是其他选项
        index: child_index
      }

      this.triggerEvent('myEvent', obj);

    }
  }
})